package jzqb.service.impl;

import haha.life.common.CutPageBean;
import jzqb.bo.CommonsBo;
import jzqb.dao.AdminUserInfoDao;
import jzqb.dao.ICommonsDao;
import jzqb.po.AdminUserPo;
import jzqb.po.CommonsPo;
import jzqb.service.ICommonsService;
import jzqb.vo.CommonsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Description:
 *
 * @author BaiGe
 * @date: 2021/4/7 14:27
 * @Version 1.0
 */
@Service
public class CommonsServiceImpl implements ICommonsService {

    @Autowired
    private ICommonsDao commonsDao;

    @Autowired
    private AdminUserInfoDao adminUserInfoDao;

    @Override
    public CutPageBean<CommonsVo> queryCommons(CommonsBo commonsBo) {
        CutPageBean<CommonsVo> cutPageBean = new CutPageBean<>();
        List<CommonsVo> commonsVos = commonsDao.queryCommonsList(commonsBo);
        //全部用户
        List<AdminUserPo> allUserList = null;
        if (commonsVos != null && !commonsVos.isEmpty()) {
            allUserList = adminUserInfoDao.findAllUser();
            for (CommonsVo commons : commonsVos) {
                if (allUserList != null && !allUserList.isEmpty()) {
                    //发送人姓名
                    for (AdminUserPo user : allUserList) {
                        if (commons.getSender().equals(user.getId())) {
                            commons.setSenderName(user.getName());
                            break;
                        }
                    }

                    //回复人姓名
                    for (AdminUserPo user : allUserList) {
                        if (commons.getReplier().equals(user.getId())) {
                            commons.setReplierName(user.getName());
                            break;
                        }
                    }
                }else{
                    break;
                }
            }
        }

        int count = (int) commonsDao.queryCommonsCount(commonsBo);
        cutPageBean.initCutPage(count, commonsBo.getPageSize(), commonsVos);
        return cutPageBean;
    }

    @Override
    public Boolean updateCommons(CommonsPo commonsPo) {
        return commonsDao.updateCommons(commonsPo);
    }

    @Override
    public Boolean delCommonsById(Integer id) {
        return commonsDao.delCommonsById(id);
    }

    /*----------------------前台-------------------------*/
    @Override
    public CutPageBean<CommonsVo> queryOwnCommons(CommonsBo commonsBo) {
        CutPageBean<CommonsVo>  cutPageBean=new   CutPageBean<>();
        List<CommonsVo> commonsList = commonsDao.queryOwnCommonsList(commonsBo);
        //全部用户
        List<AdminUserPo> allUserList = null;
        if (commonsList != null && !commonsList.isEmpty()) {
            allUserList = adminUserInfoDao.findAllUser();
            for (CommonsVo commons : commonsList) {
                if (allUserList != null && !allUserList.isEmpty()) {
                    //发送人姓名
                    for (AdminUserPo user : allUserList) {
                        if (commons.getSender().equals(user.getId())) {
                            commons.setSenderName(user.getName());
                            break;
                        }
                    }

                    //回复人姓名
                    for (AdminUserPo user : allUserList) {
                        if (commons.getReplier().equals(user.getId())) {
                            commons.setReplierName(user.getName());
                            break;
                        }
                    }
                }else{
                    break;
                }
            }
        }
       int  count=(int) commonsDao.queryOwnCommonsCount(commonsBo);
        cutPageBean.initCutPage(count,commonsBo.getPageSize(),commonsList);
        return cutPageBean;
    }

    @Override
    public Boolean addCommons(CommonsPo commonsPo) {
        return commonsDao.addCommons(commonsPo);
    }

    @Override
    public Boolean updateCommonsReadStatus(CommonsPo commonsPo) {
        return commonsDao.updateCommonsReadStatus(commonsPo);
    }

    @Override
    public long getUnReadCommonsNum(CommonsBo commonsBo) {
        return commonsDao.getUnReadCommonsNum(commonsBo);
    }
}
